
--[[
	消息处理

--]]
---------------------------------------------------------
-- class
---------------------------------------------------------
local Handler = class("Handler")

function Handler:ctor(name)
    self:register()
end


function Handler:register()
    --是覆盖common hander中的协议
    g.register("C2_TS_leave", handler(self, self.reqLeaveDesk))
    g.register("C2_TS_sit", handler(self, self.reqSitDown))
    g.register("C2_TS_standup", handler(self, self.reqStandUp))
    g.register("C2_TS_bet", handler(self, self.reqBet))
    g.register("C2_TS_buyinchips", handler(self, self.reqBuyChip))

    --g.register("match_start", handler(self, self.onMatchStart))
    g.register("merge_desk", handler(self, self.onMergeDesk))
    g.register("up_blind_time", handler(self, self.onUpBlindTime))
    g.register("match_data", handler(self, self.onMatchData))
    g.register("match_pause", handler(self, self.onMatchPause))
    g.register("match_close", handler(self, self.onMatchClose))
end

function Handler:reqLeaveDesk(msg)
    g.log:deskInfo(msg.mid,"reqLeaveDesk", msg)
    local uid = msg.uid
    local user = g.user_mgr:getUser(uid)
    if not user then --玩家不在这个桌子里
        g.log:deskInfo(uid,"reqLeaveDesk, not in desk", msg)
        return {code = g.error_code.desk_user_err, tid = g.desk_id}
    end
    if g.desk.leaveCallback then
        g.desk:leaveCallback(user) --让各个游戏做它要做的事
    end
    user:setDisconnect(true)
    return {
        code = 0, tid = g.desk_id,
    }
end

function Handler:reqSitDown(msg)
    g.log:deskInfo(msg.mid, "reqSitDown in match desk", msg)
    return {code=g.error_code.fail}
end

function Handler:reqStandUp(msg)
    g.log:info("reqStandUp in match desk", msg)
    return {code=g.error_code.fail}
end

function Handler:reqBuyChip(msg)
    g.log:deskInfo(msg.mid, "reqBuyChip in match desk", msg)
    return {code=g.error_code.fail}
end

-- 玩家下注
function Handler:reqBet(msg)
    --print("##########################bet########", tostring(msg))
    local uid = msg.mid
    local user = g.user_mgr:getUser(uid)
    if not user then
        return {code = g.error_code.desk_user_err, tid = g.desk_id}
    end
    user:setNoOperNum(0)
    if user:isRobot() then
        --g.robo_oper:betChip(user)
        return {code = g.error_code.ok}
    end
    return g.desk:onBet(uid, msg)
end


-- 合桌, 别的桌子玩家合到这个桌子
function Handler:onMergeDesk(msg)
    g.log:deskInfo(msg.mid,"onMergeDesk ", msg)
    --先把要合桌的人放到一个列表，等游戏一局结束后进行合桌
    --合桌对，座位有一定的要求， 庄，大小盲间不能有空位
    local uid = msg.mid
    local user = g.user_mgr:getUser(uid)
    if not user then
        user = g.user_mgr:addUser(uid, msg.user_data)--在线人数+1
    else
        user:initUser(msg.user_data)
    end
    user:setDisconnect(false)
    user:setInfo(msg.info)
    user:setChip(msg.chips)
    if msg.chips == 0 then
        --旁观者不用坐下
        user:setStatus(user.s_status_init)
        local desk_info = g.desk:getDeskInfo(uid)
        desk_info.old_tid = msg.tid
        g.desk:sendUser(user, "TS_2C_mergedesk", desk_info)
        g.sendGames("merge_desk", {mid=uid, tid=g.tid}) --记录下用户所在tid
        return true
    end
    local msg_data = {
        mid = user:getUid(),
        tid = g.desk_id,
        rtype = g.rtype,
        chips = user:getChip(),
    }
    local res = g.chandler:reqSitDown(msg_data)
    if res.code == g.error_code.ok then
        --坐下成功
        user:setStatus(user.s_status_init)
        local desk_info = g.desk:getDeskInfo(uid)
        desk_info.old_tid = msg.tid
        g.sleep(8)
        g.desk:callUser(user, "TS_2C_mergedesk", desk_info)
        g.sendGames("merge_desk", {mid=uid, tid=g.tid}) --记录下用户所在tid
        g.log:deskInfo(msg.mid,"onMergeDesk ok")
        return true
    end
    --坐下失败了，退出桌子
    g.user_mgr:removeUser(uid)
    g.log:deskErr(msg.mid,"onMergeDesk sitdown err", res)

    return false
end

--接收比赛房间数据，开始游戏
function Handler:onMatchData(msg)
    g.desk:matchData(msg)
    return true
end

function Handler:onUpBlindTime(msg)
    g.desk:upBlindTime(msg)
    return true
end

function Handler:onMatchPause(msg)
    g.desk:matchPause(msg)
    return true
end

function Handler:onMatchClose(msg)
    g.desk:matchClose(msg)
    return true
end

return Handler